package com.kai.admin.security.handler;

import com.kai.admin.security.service.SecurityTokenService;
import com.kai.auth.config.SecurityProperties;
import com.kai.common.data.Result;
import com.kai.common.util.WebUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 退出登录处理器
 *
 * @author weijk
 * @since 2023/7/24
 */
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Resource
    private SecurityTokenService securityTokenService;

    @Resource
    private SecurityProperties securityProperties;

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        String token = httpServletRequest.getHeader(securityProperties.getTokenHeader());
        securityTokenService.deleteToken(token);
        WebUtil.writeJson(Result.success(), httpServletResponse);
    }
}
